import QtQuick

/**EMManager.qml
  * 管理UI所有的组件，数据查询请求则提交给父组建进行维护
  * 父组建使用setPatientListData, addPatientListItem, setPatientTableData设置UI渲染数据
  * startDetailPage则用于创建渲染细节界面, destroyAllWin则需要重载外部关闭事件调用,否则进程无法退出
  *
  * 简单使用
    import QtQuick
    import FluentUI

    FluWindow {
        id: root
        width: 1152
        height: 540
        visible: true
        title: qsTr("Hello World")

        closeListener: function(event){
            if(closeDestory){
                manager.destroyAllWin()
                destoryOnClose()
            }else{
                visible = false
                event.accepted = false
            }
        }

        function getSample(){
            let s = '{"res":"{\\"ori\\": {\\"file\\": \\"http://114.55.245.149:8051/gateway/result/file?path_info=56bdf47b7294f70149a24cc5ef4e115fc2c0ee4bda796be83dd17d525c21ef5f/gateway-user-cache/20240115152851_test115_right_center_1998_12_20240115152917_s.png\\", \\"seg\\": \\"http://114.55.245.149:8051/gateway/result/file?path_info=56bdf47b7294f70149a24cc5ef4e115fc2c0ee4bda796be83dd17d525c21ef5f/gateway-service-cache/mb-seg-unet-cache/20240115152851_test115_right_center_1998_12_20240115152917_s/ori/visualization/20240115152851_test115_right_center_1998_12_20240115152917_s.png\\", \\"grade\\": {\\"cam\\": \\"http://114.55.245.149:8051/gateway/result/file?path_info=56bdf47b7294f70149a24cc5ef4e115fc2c0ee4bda796be83dd17d525c21ef5f/gateway-service-cache/mb-grade/20240115152851_test115_right_center_1998_12_20240115152917_s/ori/grade/cam/20240115152851_test115_right_center_1998_12_20240115152917_s.png\\", \\"cam_plus\\": \\"http://114.55.245.149:8051/gateway/result/file?path_info=56bdf47b7294f70149a24cc5ef4e115fc2c0ee4bda796be83dd17d525c21ef5f/gateway-service-cache/mb-grade/20240115152851_test115_right_center_1998_12_20240115152917_s/ori/grade/cam_plus/20240115152851_test115_right_center_1998_12_20240115152917_s.png\\", \\"pie\\": [0.8675428032875061, 0.06645651161670685, 0.06547127664089203, 0.00037291148328222334, 0.00015648819680791348]}}, \\"enh\\": {\\"file\\": \\"http://114.55.245.149:8051/gateway/result/file?path_info=56bdf47b7294f70149a24cc5ef4e115fc2c0ee4bda796be83dd17d525c21ef5f/gateway-service-cache/mb-enhance-cache/20240115152851_test115_right_center_1998_12_20240115152917_s.png\\", \\"seg\\": \\"http://114.55.245.149:8051/gateway/result/file?path_info=56bdf47b7294f70149a24cc5ef4e115fc2c0ee4bda796be83dd17d525c21ef5f/gateway-service-cache/mb-seg-unet-cache/20240115152851_test115_right_center_1998_12_20240115152917_s/enh/visualization/20240115152851_test115_right_center_1998_12_20240115152917_s.png\\", \\"grade\\": {\\"cam\\": \\"http://114.55.245.149:8051/gateway/result/file?path_info=56bdf47b7294f70149a24cc5ef4e115fc2c0ee4bda796be83dd17d525c21ef5f/gateway-service-cache/mb-grade/20240115152851_test115_right_center_1998_12_20240115152917_s/enh/grade/cam/20240115152851_test115_right_center_1998_12_20240115152917_s.png\\", \\"cam_plus\\": \\"http://114.55.245.149:8051/gateway/result/file?path_info=56bdf47b7294f70149a24cc5ef4e115fc2c0ee4bda796be83dd17d525c21ef5f/gateway-service-cache/mb-grade/20240115152851_test115_right_center_1998_12_20240115152917_s/enh/grade/cam_plus/20240115152851_test115_right_center_1998_12_20240115152917_s.png\\", \\"pie\\": [0.7350566387176514, 0.20122385025024414, 0.06213532015681267, 0.0013915440067648888, 0.000192699721083045]}}, \\"done\\": true}"}'
            return s
        }


        EMManager{
            id: manager
            width: parent.width
            height: parent.height
            detailSource: "qrc:/ClientDesktopUI/EMAnalysisDetail.qml"

            onListItemClick: (r1, r2)=>{
                // 查询数据库并设置给tabledata
                console.log(r1, r2)
            }

            onTableItemClick: (r1, r2, r3)=>{
                // 查询数据json并提交窗体渲染请求
                console.log(r1, r2, r3)

                // 创建渲染
                startDetailPage(r1 + "/" + r2 + "/" + r3, getSample())
            }

            Component.onCompleted: {
                setPatientListData([
                    {patientName: "tacom", testId: "123"},
                    {patientName: "tacom", testId: "12345"},
                    {patientName: "ta", testId: "123"},
                    {patientName: "t", testId: "1"},
                    {patientName: "t111", testId: "1"},
                    {patientName: "t2222", testId: "1"}
                ])
                addPatientListItem({patientName: "hh", testId: "1"})
                addPatientListItem({patientName: "t", testId: "1222"})

                setPatientTableData([
                    {patientName: "tacom", testId: "123", imageName: "jja.jpg", taskStatus: "0", errMsg: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
                    {patientName: "tacom", testId: "1223", imageName: "jj2.jpg", taskStatus: "0", errMsg: ""},
                    {patientName: "tacom", testId: "12223", imageName: "1jj.jpg", taskStatus: "0", errMsg: ""},
                    {patientName: "tacom", testId: "1224", imageName: "jj.jpg", taskStatus: "0", errMsg: ""},
                    {patientName: "tacom", testId: "12223", imageName: "j3j.jpg", taskStatus: "0", errMsg: ""},
                    {patientName: "tacom", testId: "12223", imageName: "j8j.jpg", taskStatus: "0", errMsg: ""},
                    {patientName: "tacom", testId: "12223", imageName: "j74j.jpg", taskStatus: "0", errMsg: ""},
                    {patientName: "tacom", testId: "12223", imageName: "j9j.jpg", taskStatus: "0", errMsg: ""},
                    {patientName: "tacom", testId: "12223", imageName: "j6j.jpg", taskStatus: "0", errMsg: ""},
                    {patientName: "tacom", testId: "12223", imageName: "j5j.jpg", taskStatus: "0", errMsg: ""},
                    {patientName: "tacom", testId: "12223", imageName: "j20j.jpg", taskStatus: "0", errMsg: ""},
                    {patientName: "tacom", testId: "12223", imageName: "jj.jpg", taskStatus: "0", errMsg: ""}
                ])
            }
        }
    }

  */

Item {
    id: root
    property var strIdMap: ({})
    property var idWinMap: ({})
    property string detailSource: ""

    signal listItemClick(string pN, string pI)
    signal tableItemClick(string pN, string pI, string iN)

    Row{
        width: parent.width
        height: parent.height
        EMPatientList{
            id: patientList
            height: parent.height
            width: 200

            onItemClick: (d1, d2)=>{
                root.listItemClick(d1, d2)
            }
        }

        EMPatientTable{
            id: paritentTable
            width: parent.width - patientList.width
            height: parent.height

            onItemClick: (r1, r2, r3)=>{
                root.tableItemClick(r1, r2, r3)
            }
        }
    }

    function setPatientListData(dataArray){
        patientList.setDataArray(dataArray)
    }

    function addPatientListItem(item){
        patientList.addData(item)
    }

    function setPatientTableData(dataArray){
        paritentTable.setDataArray(dataArray)
    }

    function startDetailPage(id, jsonStr){
        if(strIdMap.hasOwnProperty(id)){
            const win = strIdMap[id]
            win.requestActivate()
        }else{
            var com = Qt.createComponent(detailSource)
            var win = com.createObject()
            win.winTitle = id
            win.closeListener = (event)=>{
                console.log("destroy win", id)
                win.destoryOnClose()
                destroyedWin(id)
            }
            win.setDrawJson(jsonStr)
            win.show()

            strIdMap[id] = win
        }
    }

    function destroyedWin(strId){
        delete strIdMap[strId]
    }

    function destroyAllWin(){
        for(var key in strIdMap){
            const win = strIdMap[key]
            win.close()
        }
    }
}
